package com.czmh.etc.onlinepubsys.controller.mingchuang;

import com.alibaba.fastjson.JSON;
import com.czmh.etc.onlinepubsys.entity.mingchuang.Device;
import com.czmh.etc.onlinepubsys.json.mingchuang.JSONResult;
import com.czmh.etc.onlinepubsys.services.mingchuang.IDeviceService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "铭创设备管理控制器")
@RequestMapping("/{version}/api/mingchang/device")
public class DeviceController {
    @Value("${code.accessKey}")
    private String accessKey;

    @Autowired
    private IDeviceService deviceService;

    @PostMapping("/list")
    public List<Device> list() {
        log.info("查询设备");
        List<Device> list = deviceService.list();
        return list;
    }

    @PostMapping("/saveOrUpdate")
    public JSONResult saveorUpdate(@RequestBody Device entity) {
        log.info("添加设备:{}", JSON.toJSONString(entity));
        JSONResult result = new JSONResult();
        if (!accessKey.equals(entity.getAccessKey())) {
            result.setCode(JSONResult.FAIL);
            result.setMessage("已将你的信息通知网警");
            return result;
        }

        deviceService.saveOrUpdate(entity);
        return result;
    }

    @PostMapping("/delete")
    public JSONResult delete(@RequestBody Device entity) {
        log.info("删除设备:{}", JSON.toJSONString(entity));
        JSONResult result = new JSONResult();
        if (!accessKey.equals(entity.getAccessKey())) {
            result.setCode(JSONResult.FAIL);
            result.setMessage("已将你的信息通知网警");
            return result;
        }
        deviceService.delete(entity.getId());
        return result;
    }
}
